﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace httpSentry
{
    public static class XMLConfiguration
    {
        public static void Save(string Path, Dictionary<string,string> configSettings)
        {
            XmlDocument ConfigDocument = new XmlDocument();
            XmlElement ConfigElement = ConfigDocument.CreateElement("config");
            XmlElement newElement;
            foreach (string keyName in configSettings.Keys)
            {
                newElement = ConfigDocument.CreateElement(keyName);
                newElement.InnerText = configSettings[keyName];
                ConfigElement.AppendChild(newElement);
            }

            ConfigDocument.AppendChild(ConfigElement);

            ConfigDocument.Save(
                string.Format("{0}\\user_data.xml", Path));
        }

        public static Dictionary<string, string> Load(string Path, string[] Names)
        {
            if (!File.Exists(string.Format("{0}\\user_data.xml", Path)))
                return null;

            Dictionary <string, string> config = new Dictionary<string,string>();

            XmlDocument ConfigDocument = new XmlDocument();
            ConfigDocument.Load(
                string.Format("{0}\\user_data.xml", Path));

            XmlElement RootElement = ConfigDocument.DocumentElement;
            foreach (string Name in Names)
            {
                config.Add(Name, RootElement[Name].InnerText);
            }

            return config;
        }
    }
}
